#include "Image/ImageService.h"

#include "Image/ImageRGBA8.h"

#include "Core/LogService.h"

#include <freeimage.h>

namespace Orca {

	//--------------------------------------------------------------------------
	ORCA_IMPLEMENT_SERVICE(ImageService)
	
	//--------------------------------------------------------------------------
	ImageService::ImageService() {
	}

	//--------------------------------------------------------------------------
	ImageService::~ImageService() {
	}

	//--------------------------------------------------------------------------
	bool ImageService::startup() {

		FreeImage_Initialise();

		LOG_MSG(Str("ImageService: using FreeImage ") + FreeImage_GetVersion());

		return true;
	}

	//--------------------------------------------------------------------------
	void ImageService::step(float dt) {

	}

	//--------------------------------------------------------------------------
	void ImageService::shutdown() {

		FreeImage_DeInitialise();
	}

	//--------------------------------------------------------------------------
	Image* ImageService::createImage(const Str &imgType, uint32 w, uint32 h) {

		if(ImageRGBA8::TYPE_NAME == imgType)
			return new ImageRGBA8(w, h);

		return NULL;
	}

	//--------------------------------------------------------------------------
	Image* ImageService::createImageFromFile(const Str &filename) {

		FREE_IMAGE_FORMAT fmt = FreeImage_GetFileType(filename.c_str(), 0);
		FIBITMAP *dib = FreeImage_Load(fmt, filename.c_str(), 0);

		unsigned int bpp = FreeImage_GetBPP(dib);

		Image *img = NULL;

		if(24 == bpp || 32 == bpp) {
			img = ImageRGBA8::create(dib);
		}

		if(NULL == img) {
			LOG_ERR("ImageService: createImageFromFile failed");
		}

		FreeImage_Unload(dib);

		return img;
	}

	//--------------------------------------------------------------------------

}